package httpCtrler

import (
	"container/list"

	"fmt"
	"net/http"
	"time"

	jwt "github.com/appleboy/gin-jwt"
	"github.com/gin-gonic/gin"
)

var l = list.New().Init()
var LogMap = make(map[string]string)

func LogMiddleware(c *gin.Context) {
	clm := jwt.ExtractClaims(c)
	userName := clm[SessUserAccount].(string)
	str := c.Request.URL.Path
	if str == "/myLogger" {
		return
	}
	t := time.Now().Format("2006-01-02 15:04:05") + " : " + userName + " : " + LogMap[str]
	if l.Len() < 101 {
		l.PushFront(t)
	} else {
		l.Remove(l.Back())
		l.PushFront(t)
	}
	fmt.Println("logger: ", l.Back().Value)
}

func MyLogger(c *gin.Context) {
	tempSlice := make([]string, 0, 100)
	for i := l.Front(); i != nil; i = i.Next() {
		tempSlice = append(tempSlice, i.Value.(string))
	}
	fmt.Println("list : ", tempSlice)
	c.JSON(http.StatusOK, &MsgReturn{Msg: http.StatusText(http.StatusOK), Data: tempSlice})
}
